package mongo

import (
	"context"
	"fmt"
	"time"

	"github.com/spf13/viper"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

var (
	mongodb *mongo.Client
)

func Init() (err error) {
	mongodb, err = mongo.Connect(context.TODO(), options.Client().ApplyURI(viper.GetString("mongo.mongodbUri")), options.Client().SetConnectTimeout(time.Duration(viper.GetInt64("mongo.mongodbConnectTimeout"))*time.Millisecond))
	if err != nil {
		fmt.Println(err)
		return
	}
	// defer client.Disconnect(ctx)
	return
}

func Close() {
	mongodb.Disconnect(context.Background())
}
